package com.sicnustray.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sicnustray.jwt.JwtUtil;
import com.sicnustray.common.Constants;
import com.sicnustray.controller.dto.UserDTO;
import com.sicnustray.entity.Menu;
import com.sicnustray.entity.User;
import com.sicnustray.exception.ServiceException;
import com.sicnustray.mapper.RoleMapper;
import com.sicnustray.mapper.RoleMenuMapper;
import com.sicnustray.mapper.UserMapper;
import com.sicnustray.service.IMenuService;
import com.sicnustray.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yl
 * @since 2022-11-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private IMenuService menuService;
    @Resource
    private UserMapper userMapper;

    @Override
    public UserDTO login(UserDTO userDTO) {
        User one=getUserInfo(userDTO);
        if(one!=null){
            BeanUtil.copyProperties(one,userDTO,true);
            //设置token
            String token= JwtUtil.sign(one.getUsername(),one.getPassword());
            userDTO.setToken(token);
            String role=one.getRole();
            List<Menu> roleMenus= getRoleMenus(role);

            userDTO.setMenus(roleMenus);
            return userDTO;
        }else{
            throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
        }
    }

    @Override
    public User register(UserDTO userDTO) {
        //User one=getUserInfo(userDTO);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",userDTO.getUsername());
        User one=getOne(queryWrapper);
        if(one==null){
            one= new User();
            String role="普通用户";
            MD5 md5 = new MD5();
            String userPassword = md5.digestHex(userDTO.getPassword());
            userDTO.setPassword(userPassword);
            userDTO.setRole(role);
            BeanUtil.copyProperties(userDTO,one,true);
            save(one);
        }else{
            throw new ServiceException(Constants.CODE_600,"用户已存在");
        }
        return one;
}

    @Override
    public List<User> listUser() {
        return userMapper.listUser();
    }

    @Override
    public User getByUsername(String username) {

        return userMapper.selectUserByName(username);
    }

    @Override
    public List<Menu> getRoleMenus(String role) {

            Integer roleId=roleMapper.selectByRole(role);
            List<Integer> menuIds=roleMenuMapper.selectByRoleId(roleId);
            //查询所有菜单
            List<Menu> menus=menuService.findMenus("");

            List<Menu> roleMenus=new ArrayList<>();
            //筛选当前用户菜单
            for(Menu menu : menus){
                if(menuIds.contains(menu.getId())){
                    roleMenus.add(menu);
                }
                List<Menu> children=menu.getChildren();
                children.removeIf(child->!menuIds.contains(child.getId()));
            }
            return roleMenus;
        }


    private User getUserInfo(UserDTO userDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        MD5 md5 = new MD5();
        String userPassword = md5.digestHex(userDTO.getPassword());
        userDTO.setPassword(userPassword);
        queryWrapper.eq("username", userDTO.getUsername());
        queryWrapper.eq("password", userDTO.getPassword());
        User one;
        try {
            one = getOne(queryWrapper);//数据查询用户
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }


}